package com.example.smartcity_poverty.ui.AllService.PetHospital

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.PhAllListModel
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.PhMeListAllModel
import com.example.smartcity_poverty.logic.ServiceBean.PetHospitalBean.PhMoreAllModel
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.PetHospital.activity.JsThisAddActivity
import com.example.smartcity_poverty.ui.AllService.PetHospital.adapter.PhAllListAdapter
import com.example.smartcity_poverty.ui.AllService.PetHospital.adapter.PhHomeMoreAdapter
import com.example.smartcity_poverty.ui.AllService.PetHospital.adapter.PhMeListAdapter
import kotlinx.android.synthetic.main.activity_pet_hospital.*
import kotlin.coroutines.coroutineContext

/**
 * 宠物医院
 */
class PetHospitalActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_pet_hospital)
        title = "宠物医院"

        runThread()

        // 点击进入找医生页面
        jsHomeOk.setOnClickListener {
            startActivity(Intent(this, JsThisAddActivity::class.java))
        }
    }

    /**
     * 启动协程
     */
    private fun runThread() = coroutine {
        // More
        setMore()

        // MeList
        setMeList()

        // 问诊案例列表
        setAllList()
    }


    /**
     * 获取所有宠物分类
     */
    private val arrayMore = ArrayList<PhMoreAllModel.DataBean>()
    private suspend fun setMore() {
        if (getToken().isNotEmpty()) {
            api.getPhMoreAll(getToken()).apply {
                if (this.code == 200) {
                    arrayMore.addAll(this.data!!)
                    runOnUiThread {
                        phHomeMoreRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = GridLayoutManager(context, 5)
                            this.adapter = PhHomeMoreAdapter(arrayMore, R.layout.re_ph_home_more)
                            this.adapter?.notifyItemChanged(arrayMore.size)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        } else runOnUiThread { "请先登录！".show() }
    }


    /**
     * 我的问诊列表
     */
    var arrayMeList = ArrayList<PhMeListAllModel.RowsBean>()
    private suspend fun setMeList() {
        if (getToken().isNotEmpty()) {
            api.getPhMeListAll(getToken()).apply {
                if (this.code == 200) {
                    // 添加数据到集合
                    arrayMeList.addAll(this.rows!!)
                    runOnUiThread {
                        phHomeMeListRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = PhMeListAdapter(arrayMeList, R.layout.re_ph_me_list)
                            // 刷新数据
                            this.adapter?.notifyItemChanged(arrayMeList.size)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        } else runOnUiThread { "请先登录".show() }
    }


    /**
     * 问诊案例列表
     */
    private var arrayAllList = ArrayList<PhAllListModel.RowsBean>()
    private suspend fun setAllList() {
        if (getToken().isNotEmpty()) {
            api.getPhAllList(getToken()).apply {
                if (this.code == 200) {
                    arrayAllList.addAll(this.rows!!)

                    // 更新Ui
                    runOnUiThread {
                        jsAllListRv.apply {
                            this.isNestedScrollingEnabled = false
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = PhAllListAdapter(arrayAllList, R.layout.re_ph_me_list)
                            // 刷新
                            this.adapter?.notifyItemChanged(arrayAllList.size)
                        }
                    }
                } else runOnUiThread { this.msg?.show() }
            }
        } else runOnUiThread { "请先登录".show() }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}